﻿using Api.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Api.Web.helper
{
    public static class HtmlExtend
    {
        public static string SubStr(this HtmlHelper helper, object obj, int length = 0)
        {
            if (obj == null || string.IsNullOrEmpty(obj.ToString())) return string.Empty;
            var str = obj.ToString();
            if (str.Length > length && length != 0)
            {
                return str.Substring(0, length);
            }
            return str;
        }

        public static string GetParameterType(this HtmlHelper helper, object obj)
        {
            if (obj == null || string.IsNullOrEmpty(obj.ToString())) return string.Empty;
            if (obj.ToString() == ParameterType.Query.ToString())
            {
                return "查詢時參數";
            }
            else if (obj.ToString() == ParameterType.Return.ToString())
            {
                return "返回參數";
            }
            return "未知類型參數";
        }

        public static IHtmlString CusRaw(this HtmlHelper helper, object obj, int? length = null)
        {
            if (obj == null) return helper.Raw(string.Empty);
            string str = obj.ToString();
            if (string.IsNullOrEmpty(str)) return helper.Raw(string.Empty);
            if (length.HasValue && length > 0)
            {
                if (str.Length > length.Value)
                {
                    str = str.Substring(0, length.Value);
                }
            }
            str = HttpUtility.HtmlDecode(str);
            //作用是使得前台浏览器能将后台返回的字符串解析能html，而不是将其看做是string（这样是不能解析的）这就是Html.Raw方法的作用
            return helper.Raw(str);
        }
    }
}